package cn.kgc.service.impl;

import cn.kgc.dto.PmsAttrAddDTO;
import cn.kgc.dto.PmsAttrUpdateDTO;
import cn.kgc.entity.PmsAttrAttrgroupRelation;
import cn.kgc.entity.PmsAttrGroup;
import cn.kgc.mapper.PmsAttrAttrgroupRelationMapper;
import cn.kgc.mapper.PmsAttrGroupMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.mapper.PmsAttrMapper;
import cn.kgc.entity.PmsAttr;
import cn.kgc.service.PmsAttrService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (PmsAttr)表服务实现类
 *
 * @author makejava
 * @since 2022-11-29 15:49:23
 */
@Service("pmsAttrService")
public class PmsAttrServiceImpl extends ServiceImpl<PmsAttrMapper, PmsAttr> implements PmsAttrService {
    @Resource
    private PmsAttrMapper pmsAttrMapper;
    @Resource
    private PmsAttrAttrgroupRelationMapper pmsAttrAttrgroupRelationMapper;
    @Resource
    private PmsAttrGroupMapper pmsAttrGroupMapper;

    @Transactional(propagation = Propagation.REQUIRED)
    public boolean deleteAttrAndRelation(Long attrId) {
        // 删除属性
        pmsAttrMapper.deleteById(attrId);
        //删除属性组
        LambdaQueryWrapper<PmsAttrAttrgroupRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmsAttrAttrgroupRelation::getAttrId, attrId);
        pmsAttrAttrgroupRelationMapper.delete(wrapper);
        return true;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updateAttrAndRelation(PmsAttrUpdateDTO pmsAttrUpdateDTO) {
        //1修改属性
        PmsAttr pmsAttr = pmsAttrMapper.selectById(pmsAttrUpdateDTO.getAttrId());
        BeanUtils.copyProperties(pmsAttrUpdateDTO, pmsAttr);
        pmsAttrMapper.updateById(pmsAttr);
        //2删除属性对应的属性组的关系
        LambdaQueryWrapper<PmsAttrAttrgroupRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PmsAttrAttrgroupRelation::getAttrId, pmsAttr.getAttrId());
        pmsAttrAttrgroupRelationMapper.delete(queryWrapper);

        List<Long> attrGroupIds = pmsAttrUpdateDTO.getAttrGroupIdList();
        insertAttrRelation(pmsAttr, attrGroupIds);
        return true;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public boolean saveAttrAndRelation(PmsAttrAddDTO pmsAttrAddDTO) {
        //新增属性
        PmsAttr pmsAttr = new PmsAttr();
        BeanUtils.copyProperties(pmsAttrAddDTO, pmsAttr);
        pmsAttrMapper.insert(pmsAttr);

        //新增属性与属性分组关联
        List<Long> attrGroupIds = pmsAttrAddDTO.getAttrGroupIdList();
        //对象创建 放在循环外面
        insertAttrRelation(pmsAttr, attrGroupIds);
        return true;
    }

    private void insertAttrRelation(PmsAttr pmsAttr, List<Long> attrGroupIds) {
        PmsAttrAttrgroupRelation pmsAttrAttrgroupRelation=new PmsAttrAttrgroupRelation();
        for (Long attrGroupId : attrGroupIds) {
            PmsAttrGroup pmsAttrGroup = pmsAttrGroupMapper.selectById(attrGroupId);
            if (pmsAttrGroup != null) {
                pmsAttrAttrgroupRelation.setId(null);
                   pmsAttrAttrgroupRelation.setAttrId(pmsAttr.getAttrId());
                     pmsAttrAttrgroupRelation.setAttrGroupId(attrGroupId);
                        pmsAttrAttrgroupRelation.setAttrSort(0);
                    pmsAttrAttrgroupRelationMapper.insert(pmsAttrAttrgroupRelation);
            }
        }
    }
}
